#import放这里只会导入一次，放函数内部的话么次都会被执行导入
import mysql.connector
from flask import Flask, render_template,request
app=Flask(__name__)
@app.route("/")
def hello() -> str:
    return 'hello world from Flash!'
@app.route("/search4",methods=['post','get'])
def do_search() -> 'html':
    phrase = request.form['phrase']
    letters=request.form['letters']
    title = '这是搜索结果的title'
    results = str(search4letters(phrase, letters))
    log_request(request,results)
    return render_template('result.html',
                           the_phrase=phrase,
                           the_letters=letters,
                           the_title=title,
                           the_result=results,)
@app.route("/entry")
def entry_page() -> 'html':
    return render_template('entry.html',the_title='WWH标题1')

def search4letters(strA:str, strB:str)->set:
    return set(strA).intersection(set(strB))



def log_request(req:'flask_request',res:str) ->None:
    dbconfig = {'host': '10.6.152.100',
                'port': '7306',
                'user': 'root', 'password': 'dev123', 'database': 'test'}
    conn = mysql.connector.connect(**dbconfig)

    cursor = conn.cursor();

    _SQL = """ insert into log
            (phrase,letters,ip,browser_string, results)
            values(%s,%s,%s,%s,%s)"""
    cursor.execute(_SQL, (req.form['phrase'],
                          req.form['letters'],
                          req.remote_addr,
                          req.user_agent.browser,
                          res,))
    conn.commit()

    cursor.close()

    conn.close()


if(__name__=='__main__'):
    app.run(debug=True)

